"""
文件名: BrowserGoodsPage.py
作用: 浏览系统中所有商品
"""
from Common.Base import Base, open_browser

goods_list_url = 'http://localhost:8080/ecshop/search.php?encode=YTozOntzOjg6ImtleXdvcmRzIjtzOjA6IiI7czoxMDoiaW1hZ2VGaWVsZCI7czo2OiLmkJzntKIiO3M6MTg6InNlYXJjaF9lbmNvZGVfdGltZSI7aToxNjMwMTMzMjg1O30='


class BrowserGoods(Base):
    """封装表现层:制作定位器"""
    goods_list = ('css selector', '.goodsItem>a>img')  # 定位页面中所有的商品
    next_page = ("link text", "下一页")  # 定位下一页

    """封装操作层: 操作元素"""

    def get_1_goods_title(self):
        """
        获取商品标题
        :return: title
        """
        goods = self.find_elements(self.goods_list)  # 定位所有商品元素
        return [i.get_attribute('alt') for i in goods]

    def click_all_goods(self):
        """点击所有商品"""
        result = 0
        while result + 1:
            goods_titles = self.get_1_goods_title()
            for i in goods_titles:
                goods_loc = ('css selector', f'img[alt="{i}"]')  # 针对单个商品编写对应的定位器
                self.click(locator=goods_loc)
                self.click_back()
            a = self.click(locator=self.next_page)
            if a == False:
                break


if __name__ == '__main__':
    bg = BrowserGoods(open_browser())
    bg.open_url(goods_list_url)
    bg.click_all_goods()
    bg.close_browser()
